package org.king.service.impl;

import org.king.entity.User;
import org.king.global.SessionContext;
import org.king.mapper.UserMapper;
import org.king.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        return userMapper.insertUser(user.getAccount(),user.getPassword(),user.getEmail(),user.getRole());
    }

    @Override
    public boolean loginUser(String account, String password) {
        User user = userMapper.getUser(account,password);
        if(user != null){
            SessionContext.getInstance().setCurrentUser(user);//设置用户身份
            return true;
        }else
            return false;
    }

    @Override
    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }

    @Override
    public List<User> getUsers() {
        return userMapper.getUsers();
    }

    @Override
    public int deleteUser(int userId) {
        return userMapper.deleteUser(userId);
    }
}
